package org.bagou.xiangs.framework_mvvm.newer_databinding

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_new_mvvm.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.bagou.xiangs.framework_mvvm.R
import org.bagou.xiangs.framework_mvvm.databinding.ActivityNewDatabindingmvvmBinding


@Route(path = "/new/activity/NewDatabindingMvvmActivity")
class DatabindingNewMvvmActivity : AppCompatActivity() {

    private var binding: ActivityNewDatabindingmvvmBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityNewDatabindingmvvmBinding>(this, R.layout.activity_new_databindingmvvm)
        MainScope().launch(Dispatchers.IO) {
        }
        getUserInfoPromote()


    }


    private fun onUpdateUI(wechatInfo: WechatInfo?) {
        binding!!.dataModel = wechatInfo

        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = RecyclerView.VERTICAL
        recycle_list.layoutManager = layoutManager
        val adapter = DataBindingNewMvvmAdapter()
        recycle_list.adapter = adapter
        adapter.addData(wechatInfo!!.list)
    }

    private fun getUserInfoPromote() {
        val viewModelProvider = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())
        val viewModel = viewModelProvider.get(DatabindingNewViewModelMvvm::class.java)
        viewModel.getUserInfoPromote(this@DatabindingNewMvvmActivity)
            .observe(this@DatabindingNewMvvmActivity,
                Observer { wechatInfo ->
                    runOnUiThread {
                        onUpdateUI(wechatInfo)
                    }
                })
    }
}
